DOCKER 101

Docker Gotchas

Docker is generally setup to run 1 command, when that command is done the container closes. So if that command is “ls” it will run ls and close. If you set it up to run a webserver it will run that webserver: notabley running the webserver never really closes on it’s own, so it stays running.

You can access the IPs of the containers that are running on that box, however devices that aren’t on there will need the ports exposed.

Definitions

Image: what the dockerfile builds: this is the template for the containers that use it

Container: The individual “server” that is running the code. So you might have 22 containers running the 1 python image.

Run a container

docker run (container name)

Tries to run it locally, if it fails it tries to DL from docker hub
If you don’t specify a name it gets a random name

Docker commands

docker container ls: Shows the running containers

docker container ls -a: See all containers, even those that aren’t running

docker run -it –name python_container python: Creates a python container named python_container and brings you to it’s cli
docker run -dt –restart [always | unless-stopped | on-failure | no]: Starts the server, keeps it running, and it will restart it always / unless you stop it / if it crashes / don’t restart it.

docker run -rm python: remove the container once it stops

docker start (container name): starts an already existing container

docker exec python_container_1 ls: Runs the ls command on the container named python_container_1 and outputs the results.

docker exec -it python_container_1  /bin/bash: gets you the bash shell of a container

docker cp python_container_1:/tmp/file.conf . : Copies from the file /tmp/file.conf out of the container to the current folder on the host device

docker cp file.conf python_container_1:/tmp/file.conf : Copies the local file.conf to the container’s tmp folder

docker stop python_container_1: stop the container

docker rm python_container_1: deletes the container

docker container prune: deletes all non-running containers

docker stats: checks CPU/Memory/etc of the containers

docker exec -dt web01 nginx-g ‘pid /tmp/nginx.pid; daemon off; &’: So this runs the command ” nginx-g ‘pid /tmp/nginx.pid; daemon off; &” on the web01 server. The -dt tells it to run in the backgroud of the docker’s CLI. The daemon off makes sure that it runs on the container’s CLI, not on as a service, as a service isn’t a running program, thus the container would close

docker inspect: shows info about the container

docker commit my_pthon_container my_python_image: copies a container to an image. This image doesn’t nessarly have all the same commands run as the original container.

docker run -p 80:81 -dt –name my_new_python_container my_python_image: Builds a container from the commit image from above, maps the hosts port 80 to the container’s port 81, runs this in the docker server’s CLI, and names the new container “my_new_python_container”

docker run --name my_web_server -dt -p 8080:80 -it -v C:\Users\dhime\Desktop\docker_mounts\nginx\conf.d:/etc/nginx/conf.d nginx: Tie my desktop conf.d folder to /etc/nginx/conf.d on the container