Generic Commands
- List objects:
docker <object> ls
- Inspect object:
docker <object> inspect <name/id>
- Remove object:
docker <object> rm <name/id>
- Remove unused objects:
docker <object> prune
Docker Images
- Image Registry: Collection of Docker images
- Types:
- Custom/Private: Hosted by organizations/companies
- Public: Hosted at hub.docker.com
- Commands:
- List images:
docker image ls
- Pull image:
docker image pull <image-name>
(e.g., docker image pull hello-world
)
- Inspect image:
docker image inspect <image-name>
(e.g., docker image inspect hello-world
)
- Remove image:
docker image rm <image-name/id>
(e.g., docker image rm hello-world
)
- Remove unused images:
docker image prune
Custom Images
- Creation: Use a
Dockerfile
- Requires a base image
- Key Commands:
FROM
: Specify base image
COPY
: Copy files from local to image (COPY <local-path> <image-path>
)
EXPOSE
: Expose port for container
RUN
: Execute commands during build
CMD
: Execute command when container starts
WORKDIR
: Set/create working directory
ENV
: Set environment variable
- Push to Docker Hub:
- Sign up at hub.docker.com (free)
- Rename image:
<docker-hub-username>/<image-name>
- Login:
docker login
- Commands:
- Build image:
docker image build -t <image-name> <context>
(e.g., docker image build -t mywebsite .
)
- Tag image:
docker image tag <old-name> <new-name>
(e.g., docker image tag mywebsite amitksunbeam/mywebsite
)
- Push image:
docker image push <image-name>
Docker Containers
- Characteristics:
- Run one application at a time
- Stop when application finishes/errors
- Data not persisted; lost if container removed
- Commands:
- List running containers:
docker container ls
- List all containers:
docker container ls -a
(Statuses: Created, Running, Exited)
- Create container:
docker container create <image-name/id>
(e.g., docker container create hello-world
)
- Start container:
docker container start <name/id>
- Stop container:
docker container stop <name/id>
- View logs:
docker container logs <name/id>
- Inspect container:
docker container inspect <name/id>
- Remove exited container:
docker container rm <name/id>
- Remove running container:
docker container rm -f <name/id>
- Run in foreground:
docker container run <image-name/id>
(e.g., docker container run httpd
)
- Run with options:
docker container run -d -i -t --name <name> -p <host-port>:<container-port> -v <volume>:<mount-path> <image-name/id>
d
: Detached mode (background)
i
: Interactive mode
t
: Allocate terminal
-name
: Set container name
p
: Publish port
e
: Set environment variable
v
: Attach volume
- Example:
docker container run -d -i -t --name myhttpd -p 8080:80 httpd
- MySQL example:
docker container run -itd --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root -v mysql-volume:/var/lib/mysql mysql
- Execute command:
docker container exec <name/id> <command>
(e.g., docker container exec myhttpd date
)
- Get terminal:
docker container exec -it <name/id> bash
(or sh
)
- Remove unused containers:
docker container prune
Docker Volumes
- Commands:
- List volumes:
docker volume ls
- Create volume:
docker volume create <volume-name>
Fordocker volume rm <volume-name>`
- Remove unused volumes:
docker volume prune