Skip to content

Instantly share code, notes, and snippets.

@thedarkzeno
Last active January 23, 2020 16:29
Show Gist options
  • Save thedarkzeno/dbf73eb6d718a5bff59a9d735e74fabc to your computer and use it in GitHub Desktop.
Save thedarkzeno/dbf73eb6d718a5bff59a9d735e74fabc to your computer and use it in GitHub Desktop.

Docker

Installing

run in the terminal:

curl -fsSL htpps://get.docker.com/ | sh

It will download the latest version (not recommended for deployment in your server, you should stay on a stable version)

Check the version

docker --version

Hello World

run in the terminal:

docker run hello-world

If it's the first time running the hello-world docker will pull the image from docker hub

after it's ready it should print

Hello from Docker.

Useful commands

  • To check the images just run
dock images
  • To check running containers run
docker ps
  • To check all containers run
docker ps -a
  • to run an interactive terminal of a container
docker run -ti yourimagename
  • to run the container in the background
docker run -d yourimagename
  • choose the version of image
docker run yourimagename:version
  • exiting the container

Press ctrl + d or ctrl + c

  • exit the container, but keep it running

press ctrl+p+q

  • enter in a running container
docker attach containerID
  • create container without executing it
docker create imagename
  • stopping a container
docker stop containerID
  • pausing container
docker pause containerID
  • unpausing container
docker unpause containerID
  • container stats
docker stats containerID
  • container processes
docker top containerID
  • container logs
docker logs containerID
  • remove container
docker rm containerID

Limiting CPU and MEM

  • to check mem info
docker inspect containerID | grep -i mem

memory: 0 means no limit

  • limit memory during container creation
docker run -ti --memory 512m --name containerName ImageName

this will create a container with 512 MB of memory limit, with the name containerName, of the image ImageName

  • update the memory limit
docker update --memory 1024m containerID

now the container can use up to 1024 MB of the ram

  • check cpu info
docker inspect containerID | grep -i cpu
  • creating container with limited cpu
docker run -ti --cpu-shares 1024 --name conainerName ImageName

this container will have 1024 shares of processing power, and the processing power will be split accordingly with all shares of all container running

  • updating container cpu limit
docker update --cpu-shares 1024m containerID

Volumes

  • create a container with external volume
docker run -ti -v /volumeName ImageName command

Dockerfile

  • Running a react app inside a container
FROM node

RUN mkdir /usr/src/app

COPY . /usr/src/app

WORKDIR /usr/src/app

ENV PATH /usr/src/app/node_modules/.bin:$PATH

RUN yarn

CMD [ "npm", "start" ]
  • build image
docker build -t imageName:version .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment