Docker, the basic commands

A summary of the most useful docker commands. How to execute bash in a container? How to export an image?

Download an image

docker pull image_name:image_version

Build an image from a Dockerfile and a context

docker build -t image_name /path/to/dockerfile/ # -t: name of the image

Listing images

docker image ls

Creation of a container

docker run -td \ -p external_port_number:internal_port_number \ --expose other_internal_port_number \ -v host_folder:container_folder \ --name container_name image_name # -d: detached mode, run the container in background. # -p: port redirection. # --expose: make available the port for other containers but not to the host. # The -p option automatically expose the port. # -v: create a volume on the host and mount it into the container. # You can add mounting options like ":ro", ":delegated". # Read the doc for more info.

Listing the running containers

docker ps

Listing the running and stopped containers

docker ps --all

Executing a command in a container

docker exec container_name command # example: docker exec 5fc5f10f9fa0 ls

Executing an interactive bash shell in a container

docker exec -it container_name bash # -i: interactive mode # -t: tty

Start and stop a container

docker start my_container docker stop my_container

Commit the container's content to an image

docker commit container_name image_name

Delete a container

docker container rm container_name # or its alias docker rm container_name # remove all stopped containers docker container prune

Delete an image

docker image rm image_name # or its alias docker rmi image_name

Export and import an image from an archive

docker save --output archive_name.tar image_name docker load --input archive_name.tar

Get the details of a container

docker inspect container_name

Display the logs of a container

docker logs container_name


Continue your apprenticeship of docker in the tutorial Docker Compose, what is it?.