Docker is the most popular container platform. A container is a standard unit of software that packages up code and all its dependencies so the application can run quickly and reliably in different computer environments. In this tutorial you will learn all the basics of Docker.

Table of Contents

In other words, you can run software in an isolated way, without affecting your system files, quickly because you don’t need to install all required packages every time you use a different computer, and reliably because it will always run the same way.

Install Docker

Check Install Docker.

Run a container

docker run <image>
  • You can search available images in Docker Hub.
  • Images can have several versions, these “versions” are called “tags” and are defined after a colon (“:”) at the end of the image name. For example, if you want to use Ubuntu 18.04:
    docker run -it ubuntu:18.04
    

Most used “docker run” parameters

  • --name <container name>: add a name to the container.
  • -p <host-port>:<container-port>: this parameter will make accesible a container port to the host.
    docker run -p 80:80 nginx
    
    • If you want to publish an UDP port, you have to add /udp to the end.
      docker run -p 1080:1080/udp <some-image>
      
    • If you only add one port (-p 80), host assigned port will be a random one. You can check assigned port with docker ps or docker port <container-name> <container-port>.
  • -it: Useful when you need to access container Shell (bash, sh,…). “i” means “interactive” and “t” means “tty console”.
  • To go out from container shell without exiting, press Ctrl+p Ctrl+q.
  • -d: Run the container in the “detach” mode, meaning it will run in the background.
    • When using operating system images (like ubuntu), if you want the container to run in the background, you need to add -itd.
    • To “attach” to a detached container, run: docker attach <container-name>.
  • --rm: it will automatically delete the container when you close it.
  • -v <host-folder>:<container-folder>: create a shared folder between the host and the container. This will automatically remove the contents on container folder (and create container folder if it doesn’t exist). Use full paths.
    docker run -d --name nginx -p 80:80 -v /home/user/Documents/nginx-container:/usr/share/nginx/html nginx
    
    • Add :ro at the end of container folder to make it read-only (you can’t edit container files from the container itself, only from the host).
      docker run -d -p 80:6901 -v /dev/shm:/dev/shm --name ubuntudesktop accetto/ubuntu-vnc-xfce-firefox-g3
      
    • In this case, -v /dev/shm:/dev/shm helps to increase shared memory on container (by using host’s shared memory), that could be too small for using in containers with desktop environments. It could be better to use --shm-size <bytes> instead.
  • -m <bytes>: set a memory limit.
  • --user <username or UID>: run the container as a user different than root.
  • -e <list>: set environment variables.
    docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
    

Execute commands in the container

docker exec <container-name> <command>
  • You can use -it or -d like in docker run.

List containers

docker container ls
  • This will list running containers. To list all containers, add --all.

Stop a running container

docker container stop <container-name>
# or
docker stop <container-name>

Remove a container

You can only remove stopped containers.

docker container rm <container-name>
# or
docker rm <container-name>

Get details about a container

docker inspect <container-name>

Get logs from a container

docker logs <container-name>

Networks

You can create networks, so two or more containers can connect between them.

  • Create a network
    docker network create <network-name>
    
  • Attach a network to a new container
    docker run --name <container-name> --network <network-name> <image>
    

Search remote images

docker search <search term>

List downloaded images

docker image ls
# or
docker images

Remove images

docker image rm <image-name>
# if tag used is not 'latest', you will need to add the tag
docker image rm <image-name>:<tag>
# Useful when image doesn't have tags
docker image rm <image ID>
# Remove images that are not tagged and are not referenced by any container 
docker image prune 
 
# Remove all unused images 
docker image prune -a 

Build an image

You can build your own image using a text file called “Dockerfile”. This is a sample of the content of this file:

FROM ubuntu:20.04

WORKDIR /root

RUN apt update && apt install nginx -y

COPY file.txt .

EXPOSE 80

CMD [ "/bin/bash" ]

Then, you can build your image with this command:

# If you are in the same directory as your Dockerfile
docker build -t <your-image-name> .

More info about Dockerfile and building images in this link.