https://github.com/wsargent/docker-cheat-sheet
- Getting terminal access
docker exec -it [container ID] /bin/bash
Install Docker
Docker on Mac using docker-machine
- install Docker Toolbox first
- After that, install a VM with Docker Machine using VirtualBox:
docker-machine create --driver=virtualbox default
docker-machine ls
eval "$(docker-machine env default)"
- we can set docker-machine's env variable along the way:
docker-machine create -d virtualbox
-engine-env HTTP_PROXY=
-engine-env HTTPS_PROXY=
default
- add user to docker sudo user group
sudo usermod -aG docker my_account
Docker for Mac vs docker-machine (from Docker Toolbox)
You can use Docker for Mac and Docker Toolbox together on the same machine.
- When you want to use Docker for Mac, make sure all DOCKER environment variables are unset. You can do this in bash with unset ${!DOCKER_*}.
- When you want to use one of the VirtualBox VMs you have set with docker-machine, just run aeval $(docker-machine env default) (or the name of the machine you want to target). This will switch the current command shell to talk to the specified Toolbox machine.
Docker on Linux
(based on AWS ECS docker basic intro)
sudo yum update -y
sudo yum install -y docker
sudo service docker start
sudo usermod -aG docker ec2-user # add ec2-user to docker group. Logout and login again after this.
docker info # verify docker command can run without sudo
Docker-machine
- use to provision docker host
- creating a host
- use
docker-machine create
command and specify the driver to use - driver: virtualbox / AWS / VMWare / digital ocean ...
- use
docker-machine create --driver virtualbox testhost
-
Provision hosts in the cloud:
- Each cloud provider has different options for
docker-machine create
- Each cloud provider has different options for
-
Docker machine SSH
- login using the SSH key that is created when creating the machine
docker-machine ssh host-name
docker-machine ls
Container
Create and run a container
docker [start | stop] container_name
docker run -it --name container_name image_name command
docker run [option] [image] [command] [args]
docker run ubuntu:14.04 echo "hello world"
docker run ubuntu:14.04 ps aux
create and start container, run comand, destroy container
docker run --rm -it image_name command
Run container with a terminal
docker run -it ubuntu:12.04 /bin/bash
-i: connect docker to the STDIN on the container
-t: run a terminal
the following command must be a terminal
in this way, if you exit the container, you also shutdown the container, as the bash process is the first process run in the container
Use Ctrl + P + Q to exit and prevent shutdown container
Run container in detached mode
docker run -d ubuntu:12.04 ping 127.0.0.1 -c 50
observe the log using
docker logs [container_id]
docker logs -f [container_id]
docker -e TEST_VAR=test ubuntu:12.04
add environment variable
docker inspect container_id
Other
docker run -d -P nginx
docker run -it --rm -p 8080:8080 -v /path/to/agent.jar:/agent.jar -e JAVA_OPTS='-javaagent:/agent.jar' tomcat:8.0.29-jre8
docker ps
docker ps -a # included stopped container
Additional:
- entry point?
Container troubleshoot
docker logs <container name>
docker logs -f <container name>
- map a host folder to application's log folder in the container (use Volume)
docker run -d -P -v /nginxlogs:/var/log/nginx nginx # host_dir:container_dir
docker inspect
docker inspect --format
Start and stop docker deamon ????
sudo service docker stop
sudo service docker start
sudo service docker restart
- start the docker deamon
sudo docker -d &
Image
- list images:
docker images
- download new image:
docker pull image_name
Dockerfile
FROM ubuntu:12.04
# Install dependencies
RUN apt-get update -y
RUN apt-get install -y git curl apache2 php5 libapache2-mod-php5 php5-mcrypt php5-mysql
# Install app
RUN rm -rf /var/www/*
ADD src /var/www
# Configure apache
RUN a2enmod rewrite
RUN chown -R www-data:www-data /var/www
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
EXPOSE 80
CMD ["/usr/sbin/apache2", "-D", "FOREGROUND"]
docker build -t my-dockerhub-username/amazon-ecs-sample .
docker images # verify it exists
Network
- The -p 80:80 option maps the exposed port 80 on the container to port 80 on the host system.
docker run -p 80:80 image_name
- Find Docker ip address
boot2docker ip
- If using docker-machine
docker-machine ip machine_name
Registry and Repository
Dockerfile
Volumes
http://crosbymichael.com/advanced-docker-volumes.html
Attach volumes
docker volume create
docker volume rm
docker volume ls
docker volume inspect
Map MacOS host directories as docker volumes:
docker run -v /Users/wsargent/myapp/src:/src
网友评论