Unix Container: Difference between revisions

From Objectif Client Inc
Jump to navigation Jump to search
 
(28 intermediate revisions by 2 users not shown)
Line 1: Line 1:
==Container Howto==
==Container Howto==
===Web Site===
Docker [[https://docker.com docker.com]]
Containers at Docker Store [[https://store.docker.com/ store.docker.com]]
===Install and run a container===  
===Install and run a container===  
<pre>docker run -i -t ubuntu:14:04 /bin/bash</pre>
<pre>docker run -i -t ubuntu:18:04 /bin/bash</pre>


===Create an image from a container Commit===
===Create an image from a container Commit===
"b5673dbced61" is an image id  
* "b5673dbced61" is an image id  
<pre>docker commit -m "Ubuntu 14.10 Demonstrator" -a "Author Name" b5673dbced61 demo:14.10</pre>
<pre>docker commit -m "Ubuntu 14.10 Demonstrator" -a "Author Name" b5673dbced61 demo:14.10</pre>


Line 13: Line 18:
<pre>docker rmi b5673dbced61 </pre>
<pre>docker rmi b5673dbced61 </pre>


===List containers (Running Virtual Machines)===
===Create a volume===
<pre>docker volume create --name node_red_user_data</pre>
 
===List volumes===
<pre>docker volume ls</pre>
 
===List containers (Running Virtual Machines)===  
* Running:
<pre> docker ps</pre>
<pre> docker ps</pre>
* Full List
<pre> docker ps -a</pre>
===Start and Stop a container===
* Stop
<pre>docker stop "containerId or Name"</pre>
* Start (Batch)
<pre>docker start "containerId or Name"</pre>
* Start (and login in)
<pre>docker start -i "containerId or Name"</pre>
===Update a restart option===
* no
* on-failure
* unless-stopped
* always
<pre>docker update --restart=on-failure <container></pre>
==="Relogin" (Attach) into a Container===
* "Relogin into a running container
<pre>docker attach "container Name"</pre>
===Save an image===
* "asw20/mailserver" is the image tag
<pre>docker save asw20/mailserver:latest -o mailserver.tar</pre>
===Save all images===
<pre>docker save $(docker images | sed '1d' | awk '{print $1 ":" $2 }') -o dockerimages.tar</pre>
===Restore image===
<pre>docker load -i dockerimages.tar</pre>


===Remove a container===
===Remove a container===
"d0955f21bf24" is a container Id
* "d0955f21bf24" is a container Id
<pre>docker rm d0955f21bf24</pre>
<pre>docker rm d0955f21bf24</pre>


Line 23: Line 66:
<pre>docker stop $(docker ps -a -q)
<pre>docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)</pre>
docker rm $(docker ps -a -q)</pre>
==More Advence Topics==
===Build an image from a Dockerfile===
<pre>docker build -t objectif/appname -f pathtoDockerfile</pre>
<pre>docker build -t [username]/docker-desktop git://github.com/rogaha/docker-desktop.git</pre>


===Execute more advances===
===Execute more advances===
<pre>docker run -i -t --net="bridge" --name Demo -v /opt/docker/data mindesktop:v01.01</pre>
<pre>docker run -i -t --net="bridge" --name Demo -v /opt/docker/data mindesktop:v01.01</pre>
===Dockerfile Sample===
====node Js app====
* Create a new directory
=====Create node app=====
* Create package.json
<pre>
{
  "name": "docker_web_app",
  "version": "1.0.0",
  "description": "Node.js on Docker",
  "author": "First Last <first.last@example.com>",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "express": "^4.16.1"
  }
}
</pre>
* Create server.js
<pre>
'use strict';
const express = require('express');
// Constants
const PORT = 8080;
const HOST = '0.0.0.0';
// App
const app = express();
app.get('/', (req, res) => {
  res.send('Hello world\n');
});
app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);
</pre>
=====Create Dockerfile=====
* Create a file Dockerfile
<pre>
FROM node:10
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./
RUN npm install
# If you are building your code for production
# RUN npm ci --only=production
# Bundle app source
COPY . .
EXPOSE 8080
CMD [ "node", "server.js" ]
</pre>
* Create a file .dockerignore
<pre>
node_modules
npm-debug.log
</pre>
=====Build Image=====
* Don't forget the "." at the end...
<syntaxhighlight lang="bash">
docker build -t <username>/<applicationname> .
</syntaxhighlight>
=====List Images=====
<syntaxhighlight lang="bash">
docker images
</syntaxhighlight>
=====Run Image=====
<syntaxhighlight lang="bash">
docker run <username>/<applicationname>
</syntaxhighlight>
====Ghost (Blog Server)====
<pre>
FROM komljen/nodejs
MAINTAINER Alen Komljen <alen.komljen@live.com>
ENV GHOST_VERSION 0.5.7
ENV APP_ROOT /data/app
RUN \
curl -sLO http://ghost.org/archives/ghost-${GHOST_VERSION}.zip && \
mkdir -p ${APP_ROOT} && \
unzip -uo ghost-${GHOST_VERSION}.zip -d ${APP_ROOT} && \
rm ghost-${GHOST_VERSION}.zip
RUN \
cd ${APP_ROOT} && \
npm install --production
ADD start.sh start.sh
VOLUME ["$APP_ROOT"]
RUN rm /usr/sbin/policy-rc.d
CMD ["/start.sh"]
EXPOSE 2368
</pre>
==Gui management solution for Docker==
=== Portainer ===
Web site [https://portainer.io/ https://portainer.io]
Dashboard access
[http://localhost:9000/#/dashboard http://localhost:9000/#/dashboard]
====Deployment====
<pre>docker run -d --name myPortainer -p 9000:9000 --restart always -v /var/run/docker.sock:/var/run/docker.sock -v /opt/portainer:/data portainer/portainer-ce</pre>
====Upgrade====
* Stop Portainer
* Remove Portainer
* Pull new /upgraded image
* Recreate Portainer
<pre>
docker stop myPortainer
docker rm myPortainer
docker pull portainer/portainer-ce
docker run -d --name myPortainer -p 9000:9000 --restart always -v /var/run/docker.sock:/var/run/docker.sock -v /opt/portainer:/data portainer/portainer-ce
</pre>
== Docker Server Remote Management ==
# These commands get run inside of your VM.
<pre>
# Create the directory to store the configuration file.
sudo mkdir -p /etc/systemd/system/docker.service.d
</pre>
# Create a new file to store the daemon options.
<pre>
sudo nano /etc/systemd/system/docker.service.d/options.conf
</pre>
# Now make it look like this and save the file when you're done:
<pre>
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H unix:// -H tcp://0.0.0.0:2375
</pre>
# Reload the systemd daemon.
<pre>
sudo systemctl daemon-reload
</pre>
# Restart Docker.
<pre>
sudo systemctl restart docker
</pre>

Latest revision as of 04:21, 18 February 2025

Container Howto

Web Site

Docker [docker.com]

Containers at Docker Store [store.docker.com]

Install and run a container

docker run -i -t ubuntu:18:04 /bin/bash

Create an image from a container Commit

  • "b5673dbced61" is an image id
docker commit -m "Ubuntu 14.10 Demonstrator" -a "Author Name" b5673dbced61 demo:14.10

List Images (Source to initiare a containers)

docker images

Remove an image "b5673dbced61" is an image id

docker rmi b5673dbced61 

Create a volume

docker volume create --name node_red_user_data

List volumes

docker volume ls

List containers (Running Virtual Machines)

  • Running:
 docker ps
  • Full List
 docker ps -a

Start and Stop a container

  • Stop
docker stop "containerId or Name"
  • Start (Batch)
docker start "containerId or Name"
  • Start (and login in)
docker start -i "containerId or Name"

Update a restart option

  • no
  • on-failure
  • unless-stopped
  • always
docker update --restart=on-failure <container>

"Relogin" (Attach) into a Container

  • "Relogin into a running container
docker attach "container Name"

Save an image

  • "asw20/mailserver" is the image tag
docker save asw20/mailserver:latest -o mailserver.tar

Save all images

docker save $(docker images | sed '1d' | awk '{print $1 ":" $2 }') -o dockerimages.tar

Restore image

docker load -i dockerimages.tar

Remove a container

  • "d0955f21bf24" is a container Id
docker rm d0955f21bf24

Remove all containers

docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)

More Advence Topics

Build an image from a Dockerfile

docker build -t objectif/appname -f pathtoDockerfile
docker build -t [username]/docker-desktop git://github.com/rogaha/docker-desktop.git

Execute more advances

docker run -i -t --net="bridge" --name Demo -v /opt/docker/data mindesktop:v01.01


Dockerfile Sample

node Js app

  • Create a new directory
Create node app
  • Create package.json
{
  "name": "docker_web_app",
  "version": "1.0.0",
  "description": "Node.js on Docker",
  "author": "First Last <first.last@example.com>",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "express": "^4.16.1"
  }
}
  • Create server.js
'use strict';

const express = require('express');

// Constants
const PORT = 8080;
const HOST = '0.0.0.0';

// App
const app = express();
app.get('/', (req, res) => {
  res.send('Hello world\n');
});

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);
Create Dockerfile
  • Create a file Dockerfile
FROM node:10

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

EXPOSE 8080
CMD [ "node", "server.js" ]
  • Create a file .dockerignore
node_modules
npm-debug.log
Build Image
  • Don't forget the "." at the end...
docker build -t <username>/<applicationname> .
List Images
docker images
Run Image
docker run <username>/<applicationname>

Ghost (Blog Server)

FROM komljen/nodejs
MAINTAINER Alen Komljen <alen.komljen@live.com>
ENV GHOST_VERSION 0.5.7
ENV APP_ROOT /data/app
RUN \
curl -sLO http://ghost.org/archives/ghost-${GHOST_VERSION}.zip && \
mkdir -p ${APP_ROOT} && \
unzip -uo ghost-${GHOST_VERSION}.zip -d ${APP_ROOT} && \
rm ghost-${GHOST_VERSION}.zip
RUN \
cd ${APP_ROOT} && \
npm install --production
ADD start.sh start.sh
VOLUME ["$APP_ROOT"]
RUN rm /usr/sbin/policy-rc.d
CMD ["/start.sh"]
EXPOSE 2368

Gui management solution for Docker

Portainer

Web site https://portainer.io

Dashboard access http://localhost:9000/#/dashboard

Deployment

docker run -d --name myPortainer -p 9000:9000 --restart always -v /var/run/docker.sock:/var/run/docker.sock -v /opt/portainer:/data portainer/portainer-ce

Upgrade

  • Stop Portainer
  • Remove Portainer
  • Pull new /upgraded image
  • Recreate Portainer
docker stop myPortainer
docker rm myPortainer
docker pull portainer/portainer-ce
docker run -d --name myPortainer -p 9000:9000 --restart always -v /var/run/docker.sock:/var/run/docker.sock -v /opt/portainer:/data portainer/portainer-ce

Docker Server Remote Management

  1. These commands get run inside of your VM.
# Create the directory to store the configuration file.
sudo mkdir -p /etc/systemd/system/docker.service.d
  1. Create a new file to store the daemon options.
sudo nano /etc/systemd/system/docker.service.d/options.conf
  1. Now make it look like this and save the file when you're done:
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H unix:// -H tcp://0.0.0.0:2375
  1. Reload the systemd daemon.
sudo systemctl daemon-reload
  1. Restart Docker.
sudo systemctl restart docker