当前位置: 动力学知识库 > 问答 > 编程问答 >

docker - How to RUN and Login to a container to debug ?

问题描述:

I was just trying to build my Dockerfile , look below:

FROM ubuntu:14.04

MAINTAINER Mike Dillon <[email protected]>

ENV POSTGIS_MAJOR 2.1

ENV POSTGIS_VERSION 2.1.7+dfsg-3~94.git954a8d0.pgdg80+1

# temporary instruction, i beleive all the apt-get etc commands will need to be RUN in the

# RUN -t -i ubuntu:14.04 /bin/bash

RUN apt-get install postgresql-client

RUN apt-get update && apt-get install -y --no-install-recommends postgresql-$PG_MAJOR-po$

postgis=$POSTGIS_VERSION && rm -rf /var/lib/apt/lists/*

RUN mkdir -p /docker-entrypoint-initdb.d

COPY ./initdb-postgis.sh /docker-entrypoint-initdb.d/postgis.sh

now a few lines of code are run and than i get the following error:

Need to get 2538 kB of archives.

After this operation, 12.3 MB of additional disk space will be used.

Do you want to continue? [Y/n] Abort.

The command '/bin/sh -c apt-get install postgresql-client' returned a non-zero code: 1

Now i am not concerned about the Dockerfile nor the error , infact i know what the error i s, i have not added the -y flag to the RUN apt-get install postgresql-client command. My question is about debugging , My colleague gave me the following advice to follow everytime i get an error when i try to build a Dockerfile:

The non-zero code means that there was an error running this task. To

test exactly what's wrong, you can run and login the container, and

run this command manually inside the container.

Now how can i run and login the container ? i don't quite understand that part , can anybody explain ?

网友答案:

Suppose you run the build and has issue. The docker gives your the layer ID that you can login to do the troubleshooting.

$ docker build -t test .
Sending build context to Docker daemon  2.56 kB
Step 0 : FROM ubuntu:14.04
 ---> 8251da35e7a7
Step 1 : MAINTAINER Mike Dillon <[email protected]>
 ---> Running in 85fd1216cc35
 ---> d6d68fed500b
Removing intermediate container 85fd1216cc35
Step 2 : ENV POSTGIS_MAJOR 2.1
 ---> Running in 6d0ee92229ee
 ---> 377a352cd0fa
Removing intermediate container 6d0ee92229ee
Step 3 : ENV POSTGIS_VERSION 2.1.7+dfsg-3~94.git954a8d0.pgdg80+1
 ---> Running in 40173fe1edb9
 ---> 758a2e6c90b0
Removing intermediate container 40173fe1edb9
Step 4 : RUN apt-get install postgresql-client
 ---> Running in 3ece9c7fa9df
Reading package lists...
Building dependency tree...
Reading state information...
The following extra packages will be installed:
  krb5-locales libasn1-8-heimdal libedit2 libgssapi-krb5-2 libgssapi3-heimdal
  libhcrypto4-heimdal libheimbase1-heimdal libheimntlm0-heimdal
  libhx509-5-heimdal libk5crypto3 libkeyutils1 libkrb5-26-heimdal libkrb5-3
  libkrb5support0 libldap-2.4-2 libpq5 libroken18-heimdal libsasl2-2
  libsasl2-modules libsasl2-modules-db libwind0-heimdal postgresql-client-9.3
  postgresql-client-common
Suggested packages:
  krb5-doc krb5-user libsasl2-modules-otp libsasl2-modules-ldap
  libsasl2-modules-sql libsasl2-modules-gssapi-mit
  libsasl2-modules-gssapi-heimdal postgresql-9.3 postgresql-doc-9.3
The following NEW packages will be installed:
  krb5-locales libasn1-8-heimdal libedit2 libgssapi-krb5-2 libgssapi3-heimdal
  libhcrypto4-heimdal libheimbase1-heimdal libheimntlm0-heimdal
  libhx509-5-heimdal libk5crypto3 libkeyutils1 libkrb5-26-heimdal libkrb5-3
  libkrb5support0 libldap-2.4-2 libpq5 libroken18-heimdal libsasl2-2
  libsasl2-modules libsasl2-modules-db libwind0-heimdal postgresql-client
  postgresql-client-9.3 postgresql-client-common
0 upgraded, 24 newly installed, 0 to remove and 0 not upgraded.
Need to get 2538 kB of archives.
After this operation, 12.3 MB of additional disk space will be used.
Do you want to continue? [Y/n] Abort.
The command '/bin/sh -c apt-get install postgresql-client' returned a non-zero code: 1

So it stops at container ID (3ece9c7fa9df), then you need go with previous container (758a2e6c90b0) and login to test the command:

$ docker run -ti 758a2e6c90b0 bash
[email protected]:/# apt-get install postgresql-client
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
  krb5-locales libasn1-8-heimdal libedit2 libgssapi-krb5-2 libgssapi3-heimdal
  libhcrypto4-heimdal libheimbase1-heimdal libheimntlm0-heimdal
  libhx509-5-heimdal libk5crypto3 libkeyutils1 libkrb5-26-heimdal libkrb5-3
  libkrb5support0 libldap-2.4-2 libpq5 libroken18-heimdal libsasl2-2
  libsasl2-modules libsasl2-modules-db libwind0-heimdal postgresql-client-9.3
  postgresql-client-common
Suggested packages:
  krb5-doc krb5-user libsasl2-modules-otp libsasl2-modules-ldap
  libsasl2-modules-sql libsasl2-modules-gssapi-mit
  libsasl2-modules-gssapi-heimdal postgresql-9.3 postgresql-doc-9.3
The following NEW packages will be installed:
  krb5-locales libasn1-8-heimdal libedit2 libgssapi-krb5-2 libgssapi3-heimdal
  libhcrypto4-heimdal libheimbase1-heimdal libheimntlm0-heimdal
  libhx509-5-heimdal libk5crypto3 libkeyutils1 libkrb5-26-heimdal libkrb5-3
  libkrb5support0 libldap-2.4-2 libpq5 libroken18-heimdal libsasl2-2
  libsasl2-modules libsasl2-modules-db libwind0-heimdal postgresql-client
  postgresql-client-9.3 postgresql-client-common
0 upgraded, 24 newly installed, 0 to remove and 0 not upgraded.
Need to get 2538 kB of archives.
After this operation, 12.3 MB of additional disk space will be used.
Do you want to continue? [Y/n]

Now you see the exact same error, duplicate the problem, you can run any linux commands you can for troubleshooting in that layer of container. You should know what to do next.

In this case, when you manually run the command, and you see the error, you can run again with -y option to prove the problem can be fixed, In other case, you enable debug options, for example, in shell script, run with -x option, etc. You can also go through log files, if this helps.

分享给朋友:
您可能感兴趣的文章:
随机阅读: