Commonly-used Docker Commands: Reference and Examples

Commonly-used Docker Commands: Reference and Examples

Last updated:
Table of Contents

Start container and log in

Use docker -it <image-name>

Example: start a container based off image my-docker-app and log into it:

$ docker run -it my-docker-app

Build image from Dockerfile

Note the . at the end

$ docker image build --file path/to/Dockerfile .

List running containers

$ docker ps

Log in to running container

Suppose you have a running container whose ID is 9cc87276ac44:

$ docker exec -it 9cc /bin/bash
root@9cc87276ac44:/#

Start container map port 80

Example: start container based on image my-docker-app

$ docker run -p 80:80 my-docker-app

Start container in background

Same as above, adding d to the options:

$ docker run -d my-docker-app

List images

$ docker images

List containers

$ docker container ps --all

Troubleshooting

Cannot connect to the Docker daemon. Is the docker daemon running on this host?

Try running commands as root using sudo.

Dialogue & Discussion