How to set up docker on Ubunutu

Install docker engine

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

# Add the repository to Apt sources:
echo \
  "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

Install docker packages:

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Verify that the Docker Engine installation is successful:

sudo docker run hello-world

Install docker desktop

Solution:

$ file /etc/xdg/systemd/user/docker-desktop.service

Running the command above, you should notice that the file is a symlink to /dev/null

Delete the file: $ sudo rm /etc/xdg/systemd/user/docker-desktop.service

And then enable the service for the user: $ systemctl --user enable docker-desktop

credit to @ptarud comment on the issue.

How to use docker

Check docker image:

docker image ls

This command does the same thing as docker images and lists all the Docker images with more detailed information.

How to launch docker image and create a container?

To launch a Docker image and create a container, you can use the docker run command. Here’s a basic example of how to do this:

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
  • OPTIONS: You can specify various options to customize the container’s behavior.
  • IMAGE: The name or ID of the Docker image you want to use.
  • COMMAND: (Optional) The command to run in the container.
  • ARG...: (Optional) Arguments to pass to the command in the container.

For example:

docker run -it -v /home/zhaoliang/zzl/ref_git:/data node
  • docker run: This is the command to run a Docker container.
  • -it: These options are used together:
    • -i: It stands for “interactive” and allows you to interact with the container by providing an open stdin.
    • -t: It allocates a pseudo-TTY, which allows you to interact with the container’s shell in a more user-friendly way.
  • -v /home/zhaoliang/zzl/ref_git:/data: This is the volume mapping option. It’s used to create a bind mount between a directory on your host system (/home/zhaoliang/zzl/ref_git) and a directory within the container (/data). This means that the content of the /home/zhaoliang/zzl/ref_git directory on your host machine will be accessible and writable from within the container at the /data path. This is often used for sharing data or code between your host and the container.
  • node: This is the name of the Docker image that you want to use to create the container. In this case, it’s the “node” image, which is an official image from Docker Hub containing the Node.js runtime.

Here’s a step-by-step guide:

  1. Pull the Docker Image (if not already pulled): If the Docker image is not already available on your system, you can pull it from a Docker registry (like Docker Hub) using the docker pull command. For example:

    docker pull ubuntu:20.04
    

    This command pulls the Ubuntu 20.04 image from Docker Hub.

  2. Run the Docker Container: Use the docker run command to create and start a container based on the image. For example:

    docker run -it --name my_container ubuntu:20.04
    
    • -it: This option specifies that you want to run the container in interactive mode with a pseudo-TTY.
    • --name my_container: This option assigns the name “my_container” to the new container.
    • ubuntu:20.04: This is the name of the image you want to use.

    This will start a new container based on the Ubuntu 20.04 image and provide you with an interactive shell within the container.

  3. Interact with the Container: You can now interact with the container just like you would with a regular Linux system. You can execute commands, install software, and so on.

  4. Exit the Container: To exit the container’s shell and stop it, you can simply type exit or use the key combination Ctrl + D.

  5. List Containers: To see a list of running and stopped containers, you can use the docker ps command:

    docker ps -a
    

    This will display a list of containers, including their names, IDs, status, and more.

  6. Start and Stop Containers: You can start and stop containers using the docker start and docker stop commands:

    docker start my_container
    docker stop my_container
    
  7. Remove Containers: To remove a stopped container, you can use the docker rm command:

    docker rm my_container
    

Remember that containers are typically ephemeral, meaning any changes made within a container are not persisted unless you create an image from the container or use volumes to manage data. You can also use various options with docker run to customize container behavior, such as specifying environment variables, ports, volumes, and more, depending on your specific use case.