docker container run -it -p 80:80 nginx
docker container run -d -p 80:80 nginx
docker container run -d -p 80:80 nginx
docker container run -d -p 80:80 --name nginx-server nginx
- Looked for an image called
nginx
in the image cache - If not found in cache, it looks to the default image repo on Docker Hub
- Pulled it down (latest version), stored in the image cache
- Started it in a new container
- Specified to take port 80 on the host and forward to port 80 on the container
- Could use
docker container run --publish 8000:80 --detach nginx
to use port 8000 - Can specify versions like
nginx:1.09
docker container ls
or
docker ps
docker container ls -a
docker container stop [ID]
docker stop $(docker ps -aq)
docker container rm [ID]
docker container rm -f [ID]
docker container rm [ID] [ID] [ID]
docker rm $(docker ps -aq)
docker container logs [NAME]
docker container top [NAME]
Docker containers are often compared to virtual machines but they are actually just processes running on your host OS. In Windows/Mac, Docker runs in a mini-VM so to see the processes you'll need to connect directly to that. On Linux, however, you can run ps aux
and see the processes directly.
docker image ls
docker pull [IMAGE]
docker image rm [IMAGE]
docker rmi $(docker images -a -q)
- Images are app binaries and dependencies with metadata about the image data and how to run the image.
- Images are not a complete OS. No kernel, kernel modules (drivers).
- The host provides the kernel, a big difference between VMs.
NGINX:
docker container run -d -p 80:80 --name nginx nginx
(Note: -p 80:80
is optional as it runs on 80 by default)
APACHE:
docker container run -d -p 8080:80 --name apache httpd
MONGODB:
docker container run -d -p 27017:27017 --name mongo mongo
MYSQL:
docker container run -d -p 3306:3306 --name mysql --env MYSQL_ROOT_PASSWORD=123456 mysql
docker container inspect [NAME]
docker container inspect --format '{{ .NetworkSettings.IPAddress }}' [NAME]
docker container stats [NAME]
docker container run -it --name [NAME] nginx bash
i
= interactive: Keep STDIN open if not attachedt
= tty: Open prompt
For Git Bash, use "winpty"
winpty docker container run -it --name [NAME] nginx bash
docker container run -it --name ubuntu ubuntu
(Note: no bash
because Ubuntu uses bash by default)
docker container run --rm -it --name [NAME] ubuntu
docker container start -ai ubuntu
docker container exec -it mysql bash
docker container run -it alpine sh
(Note: use sh
because it does not include bash. Alpine uses apk
for its package manager - can install bash if you want)
In order to limit the amount of memory a Docker container process can use, simply set the -m [memory amount]
flag with the limit.
To run a container with memory limited to 256 MBs:
docker run -m 64m -d -p 8082:80 tutum/wordpress
docker container port [NAME]
docker network ls
docker network inspect [NETWORK_NAME]
(Note: "bridge" is default)
docker network create [NETWORK_NAME]
or
docker network create --driver bridge [NETWORK_NAME]
docker run -d --net=[NETWORK_NAME] --name mongodb mongo
docker container run -d --name [NAME] --network [NETWORK_NAME] nginx
docker network connect [NETWORK_NAME] [CONTAINER_NAME]
docker network disconnect [NETWORK_NAME] [CONTAINER_NAME]
docker network disconnect
To remove the network by name or id, multiple can be deleted:
docker network rm [NETWORK_NAME] [NETWORK_NAME]
docker image ls
You'll see that each image has a tag.
docker image tag nginx btraversy/nginx
docker image push bradtraversy/nginx
docker login
docker image tag bradtraversy/nginx bradtraversy/nginx:testing
FROM
- The OS used. Common is alpine, debian, ubuntu.ENV
- Environment variables.RUN
- Run commands/shell scripts, etc.EXPOSE
- Ports to expose.CMD
- Final command run when you launch a new container from image.WORKDIR
- Sets working directory (also could useRUN cd /some/path
).COPY
- Copies files from host to container.
From the same directory as Dockerfile:
docker image build -t [REPONAME] .
DOCKER_BUILDKIT=1 docker image build -t [REPONAME] .
- If you re-run the build, it will be quick because everything is cached.
- If you change one line and re-run, that line and everything after will not be cached.
- Keep things that change the most toward the bottom of the Dockerfile.
FROM nginx:latest # Extends nginx so everything included in that image is included here
WORKDIR /usr/share/nginx/html
COPY index.html index.html
docker image build -t nginx-website
docker container run -p 80:80 --rm nginx-website
docker image tag nginx-website:latest btraversy/nginx-website:latest
docker image push bradtraversy/nginx-website
docker volume ls
docker volume prune
docker pull mysql