Last active
February 6, 2023 18:30
-
-
Save sd031/9e58d4d6214336df4725526a0588d0fd to your computer and use it in GitHub Desktop.
Docker Notes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Docker: | |
"Docker is an open platform for building, shipping and running distributed applications. | |
It gives programmers, development teams and operations engineers the common | |
toolbox they need to take advantage of the distributed and networked nature of modern applications." | |
In other words , Docker is an abstraction on top of low-level operating system tools that | |
allows you to run one or more containerized processes or applications within one or more virtualized Linux instances. | |
Docker images: | |
The Docker Flow, the fundamental concept, in Docker, it all begins with an image. | |
An image is every file that makes up just enough of the operating system to do what you need to do. | |
Traditionally you'd install a whole operating system with everything for each application you do. | |
With Docker you pair it way down so that you have a little container with just enough of the operating system to | |
do what you need to do, and you can have lots and lots of these efficiently on a computer. | |
List All Docker Images: | |
command: docker images | |
List running images: | |
docker ps -a | |
to get last images runned : | |
docker ps -l | |
docker run takes an images and run into a container | |
example: docker run ubuntu:latest bash | |
check which distribution using: | |
command: cat /etc/lsb-release | |
to exit from running docker: | |
exit | |
flow till now: | |
docker image ---> docker run ---> running container ---> stopped container --- > docker commit ---> new image | |
Commit command: | |
docker commit 8c92861cf5e4 ( 8c92861cf5e4 i.e. container id) | |
above will generate a image id like this: | |
sha256:3bc95772fe9b49b137ebfdf9afda990afbaf245e03132ab592b6d632d6b7a002 | |
we use docker tag command to give images (e.g.3bc95772fe9b49b137ebfdf9afda990afbaf245e03132ab592b6d632d6b7a002 ) a name | |
docker tag 3bc95772fe9b49b137ebfdf9afda990afbaf245e03132ab592b6d632d6b7a002 my-image | |
now if you run docker images you can see new image names my-image | |
and now you can run that image like: | |
docker run -ti my-image bash | |
now best way to do is do it direct: | |
e.g. | |
docker commit happy_poltras my-image-2 | |
note: happy_poltras is the container name , and my-image-2 is the tag / new image name | |
to run any command inside container as soon as container started | |
docker run -ti ubuntu bash -c "sleep 3 , echo all done" | |
--------------------------------------------------------------------------------------- | |
attaching and detaching contained i.e. running docker in background | |
--------------------------------------------------------------------------------------- | |
docker run -d -ti ubuntu bash | |
you will see something like below: | |
7b1621cac8e64c04ea46fab6ee791b3729b21ed7d61c7a2123d0e1524f93610a | |
now when you want login in running container , use below command: | |
docker ps -l | |
it will list the last contaner runned and it will have a name (e.g. boring_mclean) | |
use that name to login in that container: | |
docker attach boring_mclean | |
now if you want to detach again without stoping it: | |
type controll +p , controll + Q | |
other way to use detached continer is | |
docker exec -ti boring_mclean bash | |
To check the container logs run below command: | |
docker logs boring_mclean | |
to remove old container | |
docker rm boring_mclean | |
-------------------- | |
enable ports: | |
-------------------- | |
docker run -ti -p 45678:45678 -p 45679:45679 --name any-server ubuntu:14.04 | |
to know the docker ip , run below command: | |
docker-machine ip | |
to see which ports are actually running | |
docker port any-server | |
to enable UDP port: | |
docker run -p 1234:1234/udp -ti ubuntu bash | |
linking two docker container: | |
first container: docker run -ti -name server ubuntu:14.04 bash | |
second container: docker run -ti --link server --name client ubuntu:14.04 bash | |
Dynamic Docker linking using docker Private network: | |
docker network create example | |
First docker: docker run -ti --net=example --name server ubuntu:14.04 bash | |
second container: docker run -ti --link server --net=example --name client ubuntu:14.04 bash | |
to only let docker work in localhost , use command below: | |
docker run -ti -p127.0.0.1:1234;1234/tcp ubuntu:14.04 bash | |
removing image from system: | |
docker rmi image-name:tag | |
To login into docker host in ssh mode run: | |
docker-machine ssh | |
Run docker with shared folder in docker host machine: | |
mkdir example | |
current directory: /home/docker | |
docker run -ti -v /home/docker/example:/shared-folder ubuntu bash | |
now if we can share that shared folder with another docker container: | |
docker run -ti -v /shared-data ubuntu bash | |
we can work and store file in directly shared-data folder | |
now we we run older image again we can work on same volume: | |
docker run -ti --volumes-from #cntainer_name ubuntu:bash | |
search image in docker: | |
docker search ubuntu | |
-------------------------------------------- | |
Building images with docker file | |
-------------------------------------------- | |
Tobuild a docker image from a docker file in current directory , use below code | |
docker build -t result_name . (here . means current directory ) | |
sample docker file | |
File name: Dockerfile | |
file contents: | |
FROM debain:sid | |
RUN apt-get -y update | |
RUN apt-get install nano | |
CMD "nano" "/tmp/notes" | |
or for example | |
========================================== | |
Product level example for running node.js application | |
========================================== | |
# using debian:jessie for it's smaller size over ubuntu | |
FROM debian:jessie | |
# Replace shell with bash so we can source files | |
RUN rm /bin/sh && ln -s /bin/bash /bin/sh | |
# Set environment variables | |
ENV appDir /var/www/app/current | |
# Run updates and install deps | |
RUN apt-get update | |
RUN apt-get install -y -q --no-install-recommends \ | |
apt-transport-https \ | |
build-essential \ | |
ca-certificates \ | |
curl \ | |
g++ \ | |
gcc \ | |
git \ | |
make \ | |
nginx \ | |
sudo \ | |
wget \ | |
&& rm -rf /var/lib/apt/lists/* \ | |
&& apt-get -y autoclean | |
ENV NVM_DIR /usr/local/nvm | |
ENV NODE_VERSION 5.1.0 | |
# Install nvm with node and npm | |
RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.29.0/install.sh | bash \ | |
&& source $NVM_DIR/nvm.sh \ | |
&& nvm install $NODE_VERSION \ | |
&& nvm alias default $NODE_VERSION \ | |
&& nvm use default | |
# Set up our PATH correctly so we don't have to long-reference npm, node, &c. | |
ENV NODE_PATH $NVM_DIR/versions/node/v$NODE_VERSION/lib/node_modules | |
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH | |
# Set the work directory | |
RUN mkdir -p /var/www/app/current | |
WORKDIR ${appDir} | |
# Add our package.json and install *before* adding our application files | |
ADD package.json ./ | |
RUN npm i --production | |
# Install forever so we can run our application | |
RUN npm i -g forever | |
# Add application files | |
ADD . /var/www/app/current | |
#Expose the port | |
EXPOSE 4500 | |
CMD ["forever", "start", "./bin/www"] | |
/////////////////////////////////////// | |
Refs: | |
https://blog.giantswarm.io/getting-started-with-docker-and-meanjs/ | |
https://blog.codeship.com/running-mean-web-application-docker-containers-aws/ | |
To run application properly , we have to run mongodb in diffderent container and connect two docker container by server name, like below: | |
first: docker run -p 27017:27017 -d --name db mongo | |
then: docker run -ti -p 3000:3000 --link db:db_1 kjbinu/bigbox bash | |
------------------------------------------------------------------ | |
How to make password protected container / image | |
Dockerfile | |
============= | |
FROM ubuntu:14.04 | |
MAINTAINER Sandip Das <[email protected]> | |
RUN apt-get update && apt-get install -y openssh-server | |
RUN mkdir /var/run/sshd | |
RUN echo 'root:W1LZSpK0Dtkb2SmJ' | chpasswd | |
RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config | |
# SSH login fix. Otherwise user is kicked off after login | |
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd | |
ENV NOTVISIBLE "in users profile" | |
RUN echo "export VISIBLE=now" >> /etc/profile | |
EXPOSE 22 | |
CMD ["/usr/sbin/sshd", "-D"] | |
docker build -t bigbox . | |
docker run -d -P --name bigbox_app bigbox | |
docker port bigbox_app 22 | |
--->0.0.0.0:32768 | |
ssh [email protected] -p 32768 | |
-------> enter password | |
W1LZSpK0Dtkb2SmJ | |
to run the final app: | |
docker run -p 27017:27017 -d --name db mongo | |
docker run -d -p 3000:3000 -P --name bigbox_app webmagician/bigbox | |
docker port bigbox_app 22 | |
--->0.0.0.0:32768 | |
ssh [email protected] -p 32768 | |
-------> enter password | |
W1LZSpK0Dtkb2SmJ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment