Димитар Георгиев

Docker

Speakings 0 комментариев
Docker
Build
Execute the following command to create and switch into a folder named test:: 
$  mkdir test && cd test
cat > Dockerfile < # Use an official Node runtime as the parent image
> FROM node:6
>
> # Set the working directory in the container to /app
> WORKDIR /app
>
> # Copy the current directory contents into the container at /app
> ADD . /app
>
> # Make the container's port 80 available to the outside world
> EXPOSE 80
>
> # Run app.js using node when the container launches 
> CMD ["node", "app.js"]
> EOF

Run the following to create the node application::
cat > app.js < const http = require('http');
>
> const hostname = '0.0.0.0';
> const port = 80;
>
> const server = http.createServer((req, res) => {
>     res.statusCode = 200;
>       res.setHeader('Content-Type', 'text/plain');
>         res.end('Hello World\n');
> });
>
server.listen(port, hostname, () => {
>     console.log('Server running at http://%s:%s/', hostname, port);
> });
>
> process.on('SIGINT', function() {
>     console.log('Caught interrupt signal and will exit');
>     process.exit();
> });
> EOF
Run this command from within the directory that has the Dockerfile::
$  docker build -t node-app:0.1 .
/The name of the image is node-app and the tag is 0.1. The tag is highly recommended when building Docker images/

look at the images you built:
$  docker images 

Run

$  docker run -p 4000:80 --name my-app node-app:0.1

test the server::
$  curl http://localhost:4000

If you want the container to run in the background (not tied to the terminal's 
session), you need to specify the -d flag.

stop and remove the container::

$  docker stop my-app && docker rm my-app 

run the following command to start the container in the background::

$  docker run -p 4000:80 --name my-app -d node-app:0.1
You can look at the logs by executing:: 
$  docker logs [container_id]


Edit app.js with a text editor of your choice ::
$ nano app.js

Build this new image and tag it with 0.2::
$ docker build -t node-app:0.2 .

 

Run another container with the new image version. Notice how we map the host's 
port 8080 instead of 80. We can't use host port 4000 because it's already in use::
$ docker run -p 8080:80 --name my-app-2 -d node-app:0.2
$ docker ps -a 

Test the containers::
$ curl http://localhost:8080
 
Debugging practices 

look at the logs of a container using docker logs [container_id]::

$ docker logs -f 91c
start an interactive Bash session inside the running container::

$ docker exec -it 91c bash /The -it flags let you interact with a container by 
allocating a pseudo-tty and keeping stdin open. Notice bash ran in the
WORKDIR directory (/app) specified in the Dockerfile/
$ ls 
Exit the Bash session:: 
$ exit

Use --format to inspect specific fields from the returned JSON. For example:
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' [container_id]
Publish

To push images to your private registry hosted by gcr, you need to tag the images
with a registry name. The format is [hostname]/[project-id]/[image]:[tag].

You can find your project ID by running:
$ gcloud config list project
$ docker images

Push ::
$ docker push gcr.io/qwiklabs-gcp-04-480e2d7515a7/node-app:0.2

Check that the image exists in gcr by visiting the image registry in your web browser::
visit: http://gcr.io/[project-id]/node-app
Stop  and remove all containers:

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

You have to remove the child images (of node:6) before you remove the node image.
Replace [project-id]::
$ docker rmi node-app:0.2 gcr.io/project-id/node-app node-app:0.1

$ docker rmi node:6 
$ docker rmi $(docker images -aq) # remove remaining images
$ docker images

At this point you should have a pseudo-fresh environment. Pull the image and run it::
$ docker pull gcr.io/[project-id]/node-app:0.2 
$ docker run -p 4000:80 -d gcr.io/qwiklabs-gcp-04-480e2d7515a7/node-app:0.2
$ curl http://localhost:4000
 
 
Назад Вперед
Войдите или зарегистрируйтесь
чтобы оставить комментарий