Docker Cheat Sheet: Commands and Concepts
This guide covers essential Docker commands, from creating containers, managing images, building Dockerfiles, to working with volumes.
🚀 Creating and Running Containers
1. Run a Container from an Image
To create and run a container from an image:
docker run image
2. Run a Container in Interactive Mode
To run a container and open its terminal:
docker run -it image
- After the container starts, you can interact with it directly in the terminal.
3. Name a Container
To give a container a specific name:
docker run --name container_name image
4. Port Mapping
To map a port from your computer to the container:
docker run -p computer_port:container_port image
- Incoming traffic to
computer_port
will be forwarded tocontainer_port
in the container.
5. Run a Container in the Background (Detached Mode)
To run the container in the background:
docker run -d image
6. Passing Environment Variables to a Container
To pass environment variables to a container:
docker run -e key=value image
🛠️ Managing Containers
7. Start a Container
To start an existing container:
docker start container_id
8. Stop a Container
To stop a running container:
docker stop container_id
9. Remove a Container
To remove a container:
docker rm container_id
- Note: The container must be stopped before removal.
10. Execute a Command in a Running Container
To start a terminal session inside a running container:
docker exec -it container_id bash
11. View Running Containers
To list all currently running containers:
docker ps
12. View All Containers (Running and Stopped)
To see all containers, including those that are stopped:
docker ps -a
🧱 Building Docker Images
13. Creating a Dockerfile
A Dockerfile
is a script used to build Docker images. Here’s an example of common Dockerfile commands:
# Set the base image for your container
FROM base_image
# Run commands during the image build process
RUN command
# Copy files from your local machine to the container
COPY source /path/in/container
# Set environment variables
ENV key=value
# Expose a port for the container
EXPOSE 8080
# Define the command to run when the container starts
CMD ["executable"]
# Optionally, run commands automatically when the container starts
ENTRYPOINT ["/path/to/script"]
# Set the working directory in the container
WORKDIR /app
# Copy all files from the local directory to the container, excluding files listed in .dockerignore
COPY . .
14. Building a Docker Image
To build a Docker image from a Dockerfile
:
docker build -t image_name /path/to/Dockerfile
📂 Managing Docker Files
15. Using the .dockerignore
File
The .dockerignore
file helps exclude unnecessary files from being copied into the Docker image, improving build speed and reducing image size:
node_modules/
.git/
16. COPY Command in Dockerfile
The COPY
command copies files from your local system to the container:
- Copy all files from the current directory to the container:
COPY . /path/in/container
- Copy a specific file:
COPY ./filename.extension /path/in/container
- Copy an entire folder:
COPY ./foldername /path/in/container
- Copy contents of a folder (not the folder itself):
COPY ./foldername/ /path/in/container
📦 Working with Volumes
17. Mounting Volumes
Volumes allow data to persist even if the container is deleted. To mount a folder from your host to the container:
docker run -it -v /path/on/host:/path/in/container image
- Example:
docker run -it -v /home/user/backup:/app/data ubuntu
- This will map
/home/user/backup
on your host to/app/data
inside the container.
Use Cases for Volumes:
- Data Persistence: Preserve data even after the container is removed.
- Backups: Easily back up container data to the host.
- Shared Data: Share data between multiple containers or between host and container.
💾 Publishing Docker Images
18. Pushing an Image to Docker Hub
To publish an image to Docker Hub:
- Tag the image with your Docker Hub username:
docker tag image_name username/image_name
- Log in to Docker Hub:
docker login
- Push the image to your repository:
docker push username/image_name
This guide gives you the essentials for working with Docker. With these commands, you’ll be able to manage containers, build images, use volumes, and more effectively.