Skip to content

Instantly share code, notes, and snippets.

@mayanksha
Last active February 1, 2020 06:51
Show Gist options
  • Save mayanksha/5eee32fe733a94bced0aaa72fa01173b to your computer and use it in GitHub Desktop.
Save mayanksha/5eee32fe733a94bced0aaa72fa01173b to your computer and use it in GitHub Desktop.
Docker Cheatsheet for Development in a standard debian environment

Intentions

I wanted to have a light-weight development setup for all my development env so that I can standardize the dev environment across all developers. Earlier, I was using an Ubuntu debootstrap by chrooting into ubuntu's filesystem. Sometimes, an application would crash.

But VMs (or whole OSes) are resource intensive and clunky, plus they have to be configured differently for different systems (if they vary in specs). So, I thought of using a Docker Container as a standard development environment. This is especially useful in case I have to install a lot of dependencies to get a project to work. I don't have to pollute my host at all, and I get the added benefit of being able to edit the file in host, while executing the programs in docker containers.

Steps to do it.

Fetch your favourite distro's Dockerfile from Docker Hub, and then do docker build . in the dir. Once you're done building image, run a container in detached mode (so that it can be later started, and reused). We also want to have a tty allocated to this container (-t flag).

docker run -t -d --name $NAME debian:stable-slim

If you then do docker container ls -a you'll see your container there and it'll be running. All you need to do now is run docker exec -it $NAME bash so as to run the command bash inside container in interactive mode (with a pseudo-tty allocated).

If your container is not running, you can do docker start --attach $NAME to start the container and then exec.

In addition to the above, you'd also want to have a persistent volume so that you can run the program inside container while writing code in host environment. You should then run the container (you'd have to delete the previously created container if their name is same) using docker run -t -d --name cowrie -v ABSOLUTE_PATH_ON_HOST:/$ABSOLUTE_PATH_ON_CONTAINER debian:stable-slim

The above steps will get you working. :)

Some Notes:

  • If you wanna save the state of a running container after you've made changes inside it, you should perform docker commit $CONT_NAME $CONT_NEW_IMAGE. This stores the old container with a new image name. You can then delete the old container and run a new container with the same name. OR, you can keep the previous container while use docker rename for the new container to rename it.

  • If you wanna use a port from host in the container, you can use the --network host parameter in docker run to directly access localhost of host. Note that this has a tremendous security implication -- The whole of the network stack of container isn't containerizes when you use this operation. This can be insecure.

  • Creating a container simply builds the filesystem layer. Starting it runs the ENTRYPOINT (or CMD) process. Run does both the create and the start, as you surmised. So you cannot "attach" to a created container... there is no process to attach to.

  • Docker attach vs exec - https://stackoverflow.com/questions/30960686/difference-between-docker-attach-and-docker-exec

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment