- 1 container has 1 process only while 1 VM can have multiple processes
-
Create container
docker create -it --name=my_container_name ubuntu bash
-
Start container
docker start my_container_name
-
Attach to container
docker attach my_container_name
-
Detach from container (escape sequence) using
<Ctrl-d>
orexit
will kill the container<Ctrl-p><Ctrl-q>
-
Stop container
docker stop my_container_name
-
Create repo in Docker Hub
-
Tag an existing image
docker tag <existing_image> <Docker Hub username>/<repo_name>:<tag_name> docker tag <existing_image> <Docker Hub username>/<repo_name>:latest
-
Login to Docker (if using private repo)
docker login --username <Docker Hub username>
-
Push to repo
docker push <Docker Hub username>/<repo_name>
-
Pull repo
docker pull <Docker Hub username>/<repo_name>:<tag_name>
If
<tag_name>
is empty, defaults tolatest
Automated script for setting up a Docker image
Standard name of Dockerfile is Dockerfile
Difference between CMD
and RUN
-
Edit
Dockerfile
FROM <image_name>:<tag_name> MAINTAINER <Name> <<email>> RUN <setup_command1> RUN <setup_command2> # Optional: Document which port will be used # Note that actual port specification should be passed through # docker run -p <local_port>:<container_port> EXPOSE <container_port> COPY <local_file_relative_to_Dockerfile> <container_file> CMD ["<runtime_cmd>", "<input>"]
Example:
FROM ubuntu:latest MAINTAINER Rufus Wong <rcywongaa@gmail.com> RUN apt update && apt install -y openssh-server RUN mkdir -p /var/run/sshd # Assuming there exists file directory/script.sh COPY directory /directory/ RUN chmod 777 /directory/script.sh CMD ["/directory/script.sh"] CMD ["echo","hello world"]
-
Build Docker image
docker build -t <image_name>:<tag_name> .
-
Run Docker image
docker run --name=my_container_name <image_name>:<tag_name>
CMD
is overrideable by thedocker run
commanddocker run --name=my_container_name <image_name>:<tag_name> echo "override CMD"
FROM ubuntu:14.04
RUN useradd --create-home --shell /bin/bash username
RUN echo "username ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
USER username
WORKDIR /home/username
By default, all docker containers are connected together via a shared virtual network
docker network ls
-
bridge
: Default name of the virtual network -
host
: ??? -
null
: ??? (seldom used)docker network inspect <virtual_network_name>
-
Gateway is the IP of the host on the virtual network
- Containers will use the host as a gateway to connect outside
Ports used by containers by default are only accessible through <container_ip>:<container_port>
To expose it to be accessible through <host_ip>:<host_port>
docker run -p <host_port>:<container_port>
docker network create <virtual_network_name>
docker network disconnect <old_network_name> <container_name>
docker network connect --ip <desired_ip> <new_network_name> <container_name>
apt install \
iputils-ping \
iproute2
docker run -it --rm --name=<container_name> <command>
-i
to create interactive session-t
to use tty--rm
to automatically remove on exit
docker exec -it <id of running container> bash
docker ps
docker container ls
docker container ls -a
docker image ls
https://hub.docker.com/search/?type=image
docker search <image name>
docker rm my_container_name # Only works if container stopped
docker rm -f my_container_name # Force remove container even if running
docker pull <image_name>
docker rmi <image_name or image ID>
docker exec <container_name> <command>
Note that variable expansion occurs outside container, to make it occur inside:
docker exec container bash -c 'echo "$ENV_VAR"'
https://github.com/15Dkatz/docker-guides
- Set up
nvidia-docker
(https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/install-guide.html#docker) - Add the following to the
.Dockerfile
ENV NVIDIA_VISIBLE_DEVICES \ ${NVIDIA_VISIBLE_DEVICES:-all} ENV NVIDIA_DRIVER_CAPABILITIES \ ${NVIDIA_DRIVER_CAPABILITIES:+$NVIDIA_DRIVER_CAPABILITIES,}graphics
- Rebuild image
- Run image with
docker run --gpus all
docker swarm init --advertise-addr=172.31.19.251
Ensure advertise-addr
is accessible externally (configure firewall inbound rules)
docker swarm join --token <refer to response from docker swarm init> 172.31.19.251:2377
docker node ls
docker service create --name=<container name> --publish=80:80 nginx
docker service update --replicas=5 <container name>
docker service ps <container name>