Docker Commands
Quick reference of essential Docker commands.
1. List Images and Containers
Display the images available on your machine.
docker images
See containers currently running.
docker ps
Include stopped containers with -a
.
docker ps -a
2. Run Containers
Start or stop existing containers.
docker start <CONTAINER_ID>
docker stop <CONTAINER_ID>
Inspect a container's logs.
docker logs <CONTAINER_ID>
3. Remove Containers and Images
Remove stopped containers.
docker rm <CONTAINER_ID>
docker rm hello-world
Remove an image you no longer need.
docker rmi <IMAGE_ID>
docker rmi hello-world:latest
4. Pull Images
Docker Hub hosts ready-to-use images.
Download the Postgres 12.0 image.
docker pull postgres:12.0
Launch a container from that image.
docker run \
--name postgres120 \
-e POSTGRES_PASSWORD=postgres \
-d \
-p 5432:5432 \
postgres:12.0
Open a shell inside the running container.
docker exec \
-it postgres120 \
/bin/bash
root@bad052b5ddae:/#
Options:
Option | Description |
---|---|
--name | Assigns the container name. |
-e | Defines environment variables. |
-d | Runs the container in detached mode. |
-p | Publishes container ports (host:container ). |
-it | Opens an interactive shell inside the container. |
5. Build Images
A Dockerfile
lists the instructions needed to build a custom image.
app
├─ Dockerfile
└─ index.py
index.py
#!/usr/bin/env python3
print("Docker es mágico!")
Dockerfile
FROM python:3
WORKDIR /usr/src/app
COPY . .
CMD [ "python", "./index.py" ]
Inside the app
directory, build the image.
docker build \
-t python-example \
.
Note: the -t
option assigns the image name and optional tag (name:tag
).
Run a container based on the newly built image.
docker run \
--name magic \
python-example
Docker es mágico!
References
Published: May 23, 2020