Skip to content

Instantly share code, notes, and snippets.

@UtkarshYadav01
Last active January 5, 2025 00:21
Show Gist options
  • Save UtkarshYadav01/c3e5907db0b780f745845cf76ad4e472 to your computer and use it in GitHub Desktop.
Save UtkarshYadav01/c3e5907db0b780f745845cf76ad4e472 to your computer and use it in GitHub Desktop.
Docker Commands
==========================================
Docker Commands
==========================================
1. docker --version
- Check version.
2. docker images
- List all Docker images.
3. docker ps
- List running containers.
4. docker pull {name}:{tag}
- Pull an image from a registry.
5. docker run {name}:{tag}
- Creates a container from a given image and starts it.
6. -d or --detach
- Runs the container in the background and prints the container ID.
7. docker logs {container}
- View logs from the service running inside the container
(which are present at the time of execution).
8. docker stop {container}
- Stop one or more running containers.
9. -p or --publish
- Publish a container's port to the host.
-p {HOST_PORT}:{CONTAINER_PORT}
10. -a or --all
- List all containers (stopped and running).
11. docker start {container}
- Start one or more stopped containers.
12. --name
- Assign a name to the container.
==========================================
Structure of a Dockerfile
==========================================
▶ **Linux operating system**
▶ **JDK21 installed**
▶ **Copy application files** from the host into the container.
▶ **Executing commands**
- **FROM**
Build this image from the specified base image.
- **RUN**
Execute any command in a shell inside the container environment.
- **COPY**
Copies files or directories from `<src>` and adds them to the container filesystem at `<dest>`.
Example: COPY `<src>` on our machine → `<dest>` in the container.
- **WORKDIR**
Sets the working directory for all following commands.
Example: Like changing into a directory using `cd...`.
- **CMD**
The instruction to be executed when a Docker container starts.
- There can only be one `CMD` instruction in a Dockerfile.
- Example: CMD ["command", "parameter"]
==========================================
Docker Image Build Command
==========================================
1. docker build {path}
- Builds a Docker image from a Dockerfile.
2. -t or --tag
- Sets a name and optionally a tag in the `name:tag` format.
==========================================
Example for Dockerfile
==========================================
▶ **Linux operating system**
Example:
FROM ubuntu:20.04
▶ **JDK21 installed**
Example:
RUN apt-get update && apt-get install -y openjdk-21-jdk
▶ **Copy application files** from the host into the container.
Example:
COPY ./app /usr/local/app
▶ **Executing commands**
Example:
RUN cd /usr/local/app && java -jar myapp.jar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment