Docker Commands
When working with Docker, we interact across multiple areas when executing code blocks or commands for container configuration. Below are the most common and useful Docker commands.
Basic Docker Commands
| Command | Description |
|---|---|
docker images | List all downloaded images. |
docker pull <image-name>:<tag> | Download an image from a repository. |
docker rmi <image-name>:<tag> | Delete a downloaded image. |
docker create --name <container-name> <image-name>:<tag> | Create a container from an image. |
docker start <id> | Start an already created container. |
docker run <id> | Create and start a container (equivalent to create + start). |
docker rm <container-name> | Delete a container. |
docker stop <container-id> | Stop a running container. |
docker ps -a | Show all containers, including stopped ones. |
docker logs --follow <container-name> | Output container logs in real time. |
docker network ls | List configured Docker networks. |
docker network create <network-name> | Create a new network. |
docker network rm <network-name> | Delete an existing network. |
docker build -t <image-name>:<my-tag> | Build an image from a Dockerfile. |
Dockerfile Structure
A Dockerfile is a text file containing instructions to build a Docker image. Below is the basic structure of a Dockerfile:
# Dockerfile
FROM <base-image-name>:<tag> # Base image
MAINTAINER <name> <email> # Maintainer information
RUN <command> # Command to execute during image build
COPY <source> <destination> # Copy files into container
ADD <source> <destination> # Similar to COPY with extra features
EXPOSE <port> # Expose a container port
CMD ["<command>", "<arguments>"] # Default command when starting container
ENTRYPOINT ["<command>", "<arguments>"] # Fixed command executed on start
Difference between CMD and ENTRYPOINT
CMD and ENTRYPOINT define the command executed when a container starts. The main difference is that CMD provides default values that can be overridden at runtime, whereas ENTRYPOINT defines a fixed command that always runs.
Structure of a docker-compose.yml
A docker-compose.yml file allows defining and running multi-container applications:
version: "3.9"
services:
backendnode:
depends_on:
- mi_postgres
build: .
ports:
- "3000:3000"
links:
- mi_postgres
mi_postgres:
image: postgres:14.18
ports:
- "5440:5432"
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
- POSTGRES_DB=test_docker
volumes:
- mi_postgres_data:/var/lib/postgresql/data
volumes:
mi_postgres_data:
Docker Compose Commands
| Command | Description |
|---|---|
docker-compose up | Start services defined in docker-compose.yml. |
docker-compose down | Stop and remove containers, networks, and volumes created by up. |
docker-compose build | Build images for services defined in docker-compose.yml. |
docker-compose logs | View logs for all services. |
docker-compose exec <service> <command> | Execute a command inside a running service container. |
Self-Assessment Quiz
Cargando cuestionario...