Add/edit files inside a docker container using a remotely? - docker

I am new to both docker and ansible.
I am using ansible-playbook to
Remotely login into a server
Pull docker images from docker hub
Create a new container.
I now want to add/edit some files to the container and run some pre-installed programs on the container.Is there a way to do so without opening the terminal and doing it manually?

add/edit some files to the container
docker cpfor copying files to the container, see the doc
http://docs.docker.com/reference/commandline/cp/
docker exec -it container_id bashand then or commands or
docker exec -it container_id sh -c awk...
see the doc
http://docs.docker.com/reference/commandline/exec/
run some pre-installed programs
docker exec -it container_id sh -c "run your program"

Related

while starting a docker container I have to execute a script inside docker container

while starting a docker container I have to execute a script inside docker container. Can I do it using docker run command or docker start command mentioning the path in docker? I know I have to use CMD in docker file but dockerfile is not present
Have you tried
docker run -it <image-name> bash "command-to-execute"
To enter a running Docker container (get a Bash prompt inside the container), please run the following:
docker container exec -it <container_id> /bin/bash
You can get the container_id by listing running Docker containers with:
docker container ps -a or docker ps -a
docker run --name TEST -d image sh -c " CMD "
in CMD section you can give the path of shell script

How to inspect the fs of a running Docker container

Is there a way to inspect a running Docker container? E.g., inspect the filesystem using a shell, etc?
To inspect an image, we could using docker run <tag> /bin/bash but I am looking to inspect a running container, not an image.
note that docker container inspect is not what I am looking for - that command just gives me metadata about the container.
Assuming that your container has a typical filesystem, you can just use docker exec to start a shell inside the container, as in:
docker exec -it mycontainer bash
Or if bash isn't available (for example, Alpine-based images):
docker exec -it mycontainer sh
Alternatively, you can export a container's filesystem as a tar archive using docker export. For example:
docker export -o mycontainer.tar mycontainer
And then you can inspect the archive or extract it as necessary. If
you just want to a file listing, then:
docker export mycontainer | tar tf -
You can use docker exec command
docker exec -it {container Id or name} command

How to list the files inside ubuntu:16.04 docker container?

I have created docker container for ubuntu:16.04 and spark.
my docker image name TestDocker
using spark application I have copied test.txt to container /opt/ml/ location.
when i am executing below command it is showing like below:
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xx.com/decision-trees-sample latest af30271f1528 19 minutes ago 959MB
then i try to execute below command
docker exec -it decision-trees-sample ls /opt/ml
it is showing error.
now I want to check file is copied or not using docker command,is it possible to check?
how can I list(LS) files contain in /opt/ml/ ?
you can launch a shell inside the container with the
docker exec
command, see the doc
https://docs.docker.com/engine/reference/commandline/exec/
For example, if the id of your container is 123abcdef, you may do either
docker exec -it 123abcdef ls /opt/ml
or open a shell inside your container
docker exec -it 123abcdef bash
and then launch ls or find or any other command
Try below steps
docker exec -it decision-trees-sample
then set the environmental variable as
export TERM=xterm
then try with
docker exec -it 123abcdef ls /opt/ml
basically it tell your which kind of terminal you are trying to open.It mainly due to some package not installed regarding terminals, you can also add it to ~/.bashrc file and commit the image.
I agree with above answers and docker exec is only for running containers but If you really want to explore container image content I would recommend using container-diff
container-diff analyze --type=file <IMG> | grep /opt/ml

How to run postgres commands in a docker container?

I don't want to install postgres locally but as I have it in my docker container, I'd like to be able to run its commands and utils, like pg_dump myschema > schema.sql.
How can I run commands related to running containers inside of them?
docker exec -it <container> <cmd>
e.g.
docker exec -it your-container /bin/bash
There are different options
You can actually copy files to docker using docker cp command. Copy required files to docker and then you can go inside the docker and run the command.
Make some modification in docker file for docker image creation. Its actually really simple to create docker file. Then using EXPOSE option you can expose a port. After that you can use docker run --publish ie.. -p option to publish a container’s port(s) to the host. Then you can access postgres from outside and run scripts from outside by creating connection.
In the first option you need go inside the containers. For that first list running dockers using docker ps command. After that you can use docker exec -it container_name /bin/bash command

Docker exec with config file outside of container

I have Docker container on my host and I want run script inside docker container with docker exec
docker exec MyContainerID python /home/myuser/my/path/my_script.py --key1=value1 --key2=value2
How can I add config file from the outside of docker container?
Is it possible do with one row command line like this command:
docker exec MyContainerID python /home/myuser/my/path/my_script --key1=value1 --key2=value2 --config=/my/path/outside/container/my_config.cfg
Do not forget that docker is about process isolation, but you can copy a file from outside the container, then
docker exec command_with_this_file
as an example, extract from
http://hub.docker.com/r/k3ck3c/captvty/
first a copy a zip file
docker cp 9087050696d4:/home/gg/Captvty/Vidéos/ ~
then extract this zip file inside the container
docker exec -it container_id unzip -d ~/Captvty ~/Téléchargements/captvty-2.3.10.zip
and reply
Yes to All

Resources