Skip to content

HOWTO dev docker_setup

steveoro edited this page Jun 18, 2021 · 4 revisions

HOW-TO: Docker: setup & useful commands

References

Ubuntu: local setup as running service

docker-ce is provided by docker.com, docker.io is provided by Debian. The first installs all the dependencies (possibly creating library duplicates), the second one typically goes the APT way and uses dynamic linking.

Make sure any old version is uninstalled:

$ sudo apt-get remove docker docker-engine docker.io

Install & enable the service:

$ sudo apt install docker.io
$ sudo systemctl start docker
$ sudo systemctl enable docker

Verify the installation:

$ docker -v
$ sudo service docker status
$ sudo docker info

Using Docker without prefixing everything with sudo

Prefixing some commands with sudo may force you to use sudo's -E parameter to pass over any environment variable you may be using. For instance, something like DB_DATA=/my_data/folder docker run mongo -d, when using sudo would need to become sudo -E DB_DATA=/my_data/folder docker run mongo -d, which is a minor inconvenience but nevertheless required.

If you want to avoid typing sudo before each command, simply add your user to the docker group.

To do that, create the group if it's missing:

$ sudo groupadd docker

Add your current user (env variable $USER) to it:

$ sudo usermod -aG docker $USER

....Then log-out & re-log in to make the changes effective.

It is also possible to run Docker in "rootless" mode, but this currently has limitations. Check out the official Docker guide on rootless mode and security.


Docker: main commands

  • docker pull image_name:versioning_tag will download on localhost from the official public Docker registry the Docker image named image_name, associated with the specified versioning_tag; for instance, docker pull mongo:4.2.6 will download the 4.2.6 version of the MongoDB image.

  • docker images gives you a list of the existing Docker images on localhost.

  • docker ps gives you a list of the running containers (ps -a to list also stopped containers).

  • docker build . creates a local Docker image from the Dockerfile in the current directory.

  • docker run --name container_name -d image_name runs a container in detached mode, (creating it with the specified name), using the Docker image_name factory image; if the -d flag is not present, the container will run in foreground and you will need to press -c to exit.

  • docker stop service_name_or_container_id stops a container running in the background or in another process.

  • docker start container_id will restart a previously stopped container.

  • docker rm container_name_or_id removes an existing container.

  • docker rmi image_name removes an existing Docker image.

  • docker exec container_name_or_id context_command will execute context_command inside the running container, whatever that may be. For example docker exec -it mongo bash will open an interactive bash terminal in a running container named "mongo" (this way, you can connect to the database running inside the container, by executing then the mongo client after the new bash shell opens up).

See the official Docker CLI docs for more information.

Clone this wiki locally