0% found this document useful (0 votes)
38 views3 pages

Essential Docker Commands Guide

The document provides a comprehensive overview of Docker commands for managing images and containers, including pulling images, running containers in different modes, and managing container states. It also covers Docker networking, Docker Compose for multi-container applications, and essential Dockerfile instructions for building images. Additionally, it explains how to push Docker images to a repository and the use of volumes for persistent data storage.

Uploaded by

asustuff15831
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views3 pages

Essential Docker Commands Guide

The document provides a comprehensive overview of Docker commands for managing images and containers, including pulling images, running containers in different modes, and managing container states. It also covers Docker networking, Docker Compose for multi-container applications, and essential Dockerfile instructions for building images. Additionally, it explains how to push Docker images to a repository and the use of volumes for persistent data storage.

Uploaded by

asustuff15831
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

docker pull image -

docker images -
docker run image - Run a container from an image
docker run -d <image>
docker run -it <image>

Note - --name <name> - to name the container

Command Mode TTY/Interactive Output shown? Use case


docker run <image> Foreground No Yes Run container and show output
docker run -d <image> Background No No Run services like web servers
docker run -it <image> Foreground Yes Yes (interactive shell) Debugging, manual
shell access

docker start <container> Start an existing container


docker stop <container> Stop a running container
docker start -a <container> Start and attach to output
docker restart <container> Restart container
docker rm <container> Remove container
docker logs <container> Show logs from container
docker exec -it <container> bash Open shell in running container

// PORT BINDING -
docker run -p <host_port>:<container_port> <image>
docker exec -it - to run commands of the container type , like MySQL cmds

//Question - How to use MySQL using Docker ?


docker pull my-sql
docker run -d -e MYSQL_ROOT_PASSWORD=secret --name using-MySQL -p 3000:3036 MySQL
docker exec -it 510a4301e912 mysql -u root -p

// Developing an Application With Docker


1. Setting Up the Network - so that container inside the n/w can communicate
a. docker network - to get all network cmds
b. docker ls - list of netwroks
c. docker network create network_name
d. - Connect a container to a network
e. - Disconnect a container from a network
f. docker network rm network_name - to remove the network
2. set up image pull and create containers ( -d for detach mode and port binding -
p host_port:cnt_port , --name custom_name , --network network_name , check for Env
var rrquired -e var1=value1 var2=value2)

// Docker Compose - is a tool for defining and running multi-container applications


Create .yaml
docker compose -f [Link] up -d
docker compose -f [Link] down

// Dockerizing your app


* Important Docker file Instructions -
What is a Dockerfile?
A Dockerfile is a script with a series of instructions that tells Docker how to:
Set up the environment
Install dependencies
Copy files
Expose ports
Run your app

| Instruction | Meaning
|
| ----------- |
-----------------------------------------------------------------------------------
--------- |
| `FROM` | Specifies the base image (e.g., [Link], Python, Ubuntu). This is
the OS + language runtime. |
| `WORKDIR` | Sets the working directory in the container (like `cd /app`).
|
| `COPY` | Copies files from your local machine to the container.
|
| `RUN` | Executes a command during the image build process (e.g., installing
packages). |
| `EXPOSE` | Documents the port your app will use (doesn't actually open it;
`docker run -p` does). |
| `CMD` | Final command to run when the container starts. Only one `CMD`
allowed. |

| Step | Question |
| --------- | ----------------------------------------------------------- |
| `FROM` | What language/runtime do I need? |
| `WORKDIR` | Where should the app live inside container? |
| `COPY` | What files do I need in the container? |
| `RUN` | What commands should run during build (e.g., install deps)? |
| `EXPOSE` | What port does the app listen on? |
| `CMD` | How do I start the app? |

docker build -t my-node-app . - To Bulid Docker image (use same dir where docker
file is there)

// How to push Docker image -


Create repo
Login With Same Cred in the Docker Cmd 2 options
docker logout
docker login -u username
or can login with provided link
docker tag my-node-app anurag053/my-node-app:latest
docker push anurag053/my-node-app:latest

* Docker file -
FROM node:18

WORKDIR /app

COPY package*.json ./
RUN npm install

COPY . .

CMD ["npm", "start"]

- NOTE - This ensures Docker only re-runs npm install if [Link] changes
(Docker layer caching optimization).
docker run -p 3000:3000 -v "${PWD}:/app" my-express-app
// Docker volumes - Volumes are persistent data stores for containers.
-v hostpath:conatinerpath

You might also like