How to run vi on docker container? - linux

I have installed docker on my host virtual machine. And now want to create a file using vi.
But it's showing me an error:
bash: vi: command not found

login into container with the following command:
docker exec -it <container> bash
Then , run the following command .
apt-get update
apt-get install vim

The command to run depends on what base image you are using.
For Alpine, vi is installed as part of the base OS. Installing vim would be:
apk -U add vim
For Debian and Ubuntu:
apt-get update && apt-get install -y vim
For CentOS, vi is usually installed with the base OS. For vim:
yum install -y vim
This should only be done in early development. Once you get a working container, the changes to files should be made to your image or configs stored outside of your container. Update your Dockerfile and other files it uses to build a new image. This certainly shouldn't be done in production since changes inside the container are by design ephemeral and will be lost when the container is replaced.

USE THIS:
apt-get update && apt-get install -y vim
Explanation of the above command
apt-get update => Will update the current package
apt-get install => Will install the package
-y => Will by pass the permission, default permission will set to Yes.
vim => Name of the package you want to install.

Your container probably haven't installed it out of the box.
Run apt-get install vim in the terminal and you should be ready to go.

Add the following line in your Dockerfile then rebuild the docker image.
RUN apt-get update && apt-get install -y vim

Alternatively, keep your docker images small by not installing unnecessary editors. You can edit the files over ssh from the docker host to the container:
vim scp://remoteuser#container-ip//path/to/document

error:: bash: vi: command not found
run the below command by logging as root user to the container--
docker exec --user="root" -it (container ID) /bin/bash
apt-get update
apt-get install vim

Use below command in Debian based container:
apt-get install vim-tiny
Complete instruction for using in Dockerfile:
RUN apt-get update && apt-get install --no-install-recommends -y \
vim-tiny \
&& apt-get clean && rm -rf /var/lib/apt/lists/*
It doesn't install unnecessary packages and removes unnecessary downloaded files, so your docker image size won't increase dramatically.

The most voted answer has the correct idea, however, it did not work in my case. The comment from #java25 did the trick in my case. I had to log into the docker container as a root user to install vim. I am just posting the comment as an answer so that it is easier for others, having the similar problem, to find it.
docker exec -ti --user root <container-id> /bin/bash
Once you are inside docker, run the following commands now to install vi.
apt-get update
apt-get install vim

To install within your Docker container you can run command
docker exec apt-get update && apt-get install -y vim
But this will be limited to the container in which vim is installed.
To make it available to all the containers, edit the Dockerfile and add
RUN apt-get update && apt-get install -y vim
or you can also extend the image in the new Dockerfile and add above command. Eg.
FROM < image name >
RUN apt-get update && apt-get install -y vim

Inside container (in docker, not in VM), by default these are not installed.
Even apt-get, wget will not work. My VM is running on Ubuntu 17.10. For me yum package manager worked.
Yum is not part of Debian or ubuntu. It is part of red-hat. But, it works in Ubuntu and it is installed by default like apt-get
To install vim, use this command
yum install -y vim-enhanced
To uninstall vim :
yum uninstall -y vim-enhanced
Similarly,
yum install -y wget
yum install -y sudo
-y is for assuming yes if prompted for any question asked after doing yum install package-name

error:: bash: vim: command not found
Run the below command by logging as root user to the container:
microdnf install -y vim

If you actually want a small editor for simple housekeeping in a docker, use this in your Dockerfile:
RUN apt-get install -y busybox && ln -s /bin/busybox /bin/vi
I used it on an Ubuntu 18 based docker.
(Of course you might need an RUN apt-get update before it but if you are making your own Docker file you probably already have that.)

Usually changing a file in a docker container is not a good idea. Everyone will forget about the change after a while. A good way is to make another docker image from the original one.
Say in a docker image, you need to change a file named myFile.xml under /path/to/docker/image/. So, you need to do.
Copy myFile.xml in your local filesystem and make necessary changes.
Create a file named 'Dockerfile' with the following content-
FROM docker-repo:tag
ADD myFile.xml /path/to/docker/image/
Then build your own docker image with docker build -t docker-repo:v-x.x.x .
Then use your newly build docker image.

Related

How to add user and a group in Docker Container running Macosx

I have a Docker container running "FROM arm64v8/oraclelinux:8" , I am running this on a Mac m1 mini using tightvnc.
I want to add a user called "suiteuser" (uid 42065) and in a group called "cvsgroup" (gid 513), inside my docker container, So that when I run the container it starts under my user directly.
Here is my entire Dockerfile-
FROM arm64v8/oraclelinux:8
# Setup basic environment stuff
ENV container docker
ENV LANG en_US.UTF-8
ENV TZ EST
ENV DEBIAN_FRONTEND=noninteractive
# Base image stuff
#RUN yum install -y zlib-devel bzip2 bzip2-devel readline-devel sqlite sqlite-devel openssl-devel vim yum-utils sssd sssd-tools krb5-libs krb5-workstation.x86_64
# CCSMP dependent
RUN yum install -y wget
RUN yum install -y openssl-libs-1.1.1g-15.el8_3.aarch64
RUN yum install -y krb5-workstation krb5-libs krb5-devel
RUN yum install -y glibc-devel glibc-common
RUN yum install -y make gcc java-1.8.0-openjdk-devel tar perl maven svn openssl-devel gcc
RUN yum install -y gdb
RUN yum install -y openldap* openldap-clients nss-pam-ldapd
RUN yum install -y zlib-devel bzip2 bzip2-devel vim yum-utils sssd sssd-tools
# Minor changes to image to get ccsmp to build
RUN ln -s /usr/lib/jvm/java-1.8.0-openjdk /usr/lib/jvm/default-jvm
RUN cp /usr/include/linux/stddef.h /usr/include/stddef.h
# Install ant 1.10.12
RUN wget https://mirror.its.dal.ca/apache//ant/binaries/apache-ant-1.10.12-bin.zip
RUN unzip apache-ant-1.10.12-bin.zip && mv apache-ant-1.10.12/ /opt/ant
ENV JAVA_HOME /usr
ENV ANT_HOME="/usr/bin/ant"
ENV PATH="/usr/bin/ant:$PATH"
CMD /bin/bash
could anyone please suggest any ideas on how to do this.
Note 1. I know its not advisable to do this directly in the container as, every time you want to make any changes you would have to rebuild it, but this time i want to do this.
To create the group:
RUN groupadd -g 513 cvsgroup
To create the user, as a member of that group:
RUN useradd -G cvsgroup -m -u 42065 suiteuser
And toward the end of Dockerfile, you can set the user:
USER suiteuser
There may be more to do here, though, depending on your application. For example, you may need to chown some of the contents to be owned by suiteuser.

Problem with Android Studio emulator in Docker container

I have a Docker container with Android studio 3.6 and it works perfectly. The problem is that the emulator does not run because the Ubuntu machine does not have the CPU to reproduce x86. Does anyone know how to include it in the Dockerfile ?. Thank you.
This is my Dockerfile:
FROM ubuntu:16.04
RUN dpkg --add-architecture i386
RUN apt-get update
# Download specific Android Studio bundle (all packages).
RUN apt-get install -y curl unzip
RUN apt-get install -y git
RUN curl 'https://uit.fun/repo/android-studio-ide-3.6.3-linux.tar.gz' > /studio.tar.gz && \
tar -zxvf studio.tar.gz && rm /studio.tar.gz
# Install X11
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get install -y xorg
# Install other useful tools
RUN apt-get install -y vim ant
# install Java
RUN apt-get install -y default-jdk
# Install prerequisites
RUN apt-get install -y libz1 libncurses5 libbz2-1.0:i386 libstdc++6 libbz2-1.0 lib32stdc++6 lib32z1
RUN apt-get install wget
RUN wget 'https://dl.google.com/android/repository/sdk-tools-linux-4333796.zip' -P /tmp \
&& unzip -d /opt/android /tmp/sdk-tools-linux-4333796.zip
RUN apt install xserver-xorg-video-amdgpu
# Clean up
RUN apt-get clean
RUN apt-get purge
ENTRYPOINT [ "android-studio/bin/studio.sh" ]
When you're using ubuntu in docker, the only way to run an android emulator is to find a system image with "arm" (e.g. system-images;android-25;google_apis;armeabi-v7a).
However, even though you're able to run emulator in the container, you will probably be disappointed about that. Since emulator based on arm is typically slow enough to boot, not to mention that running in docker could be even slower.
If you really want to create it, you can do something like below.
sdkmanager "system-images;android-25;google_apis;armeabi-v7a"
avdmanager create avd -n demoTest -d "pixel" -k "system-images;android-25;google_apis;armeabi-v7a" -g "google_apis" -b "armeabi-v7a"
emulator #demoTest -no-window -no-audio -verbose &
Once you got this prompt message
emulator: got message from guest system fingerprint HAL
Your emulator is ready to go.

Running SSH script during Microsoft Azure Web App deployment

I am deploying a web app using the Python-Django framework to Microsoft Azure.
I have succeeded in deploying it, but every time I deploy, I have to open the Azure SSH tool and run the command apt-get install libgtk2.0-dev which I gather is some Linux dependency for the opencv-python image processing library.
I wonder if there is a way to install the required software using deploy.sh files.
deploy.sh
echo "Running Linux Deployment Script..."
apt-get update && apt install -y libxrender1 libxext6
apt-get install -y libfontconfig1
apt-get install libgtk2.0-dev
Thanks in advance for your help.
You can create a script to install libgtk2.0-dev, say test.sh under /home/site.
And then add an app setting under 'Configuration' called PRE_BUILD_SCRIPT_PATH with /home/site/test.sh as the value.
You can run a script on every Webapp startup. Just adjust your script as described here: https://stackoverflow.com/a/69923647/2606766
Create a start.sh file, e.g. like this:
# install package & start app
apt-get update -y
apt install -y libxrender1 libxext6
apt-get install -y libfontconfig1
apt-get install libgtk2.0-dev
# don't forget to start your webapp service at the end of this script, e.g.:
python manage.py runserver
Set it as your startup script:
Note: There are two pitfalls to this approach:
The script must be executable, so either install w/ unix and chmod 755 start.sh or use a git command (see SO).
The packages are installed on every startup, thus you depend on external servers/repositories when starting the webapp.
You can set SCM_POST_DEPLOYMENT_ACTIONS_PATH environment variable to configure a folder. All scripts in this folder will be executed after deployment. As far as I can see this should work both on Windows and Linux.
If you need root permissions then I would suggest to use a custom docker container which has these packages already installed.
You can start by adding this command directly to the startup script, As mentioned in the answer by #HeyMan. But instead of adding a file just add the command there apt-get update && apt install -y libxrender1 libxext6 && apt-get install -y libfontconfig1 && apt-get install libgtk2.0-dev
Add this command in a single line there.
If this method also does not work for you, then you should follow the container based approach.
Create a dockerfile and add all the required dependency there.
docker build to create a docker image.
Push that image to Azure container registry using docker push
Instead of deploying from local git, deploy with help of docker.
Look at this link for help
https://learn.microsoft.com/en-us/azure/container-registry/container-registry-get-started-docker-cli?tabs=azure-cli

Installing netstat on docker linux container

I want to install netstat on my Docker container.
I looked here https://askubuntu.com/questions/813579/netstat-or-alternative-in-docker-ubuntu-server-16-04-container so I'm trying to install it like this:
apt-get install net-tools
However, I'm getting:
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package net-tools
So how can I install netstat?
You need to run apt-get update first to download the current state of the package repositories. Docker images do not include this to save space, and because they'd likely be outdated when you use it. If you are doing this in a Dockerfile, make sure to keep it as a single RUN command so that caching of the layers doesn't cache an old version of the update command with a new package install request:
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y \
net-tools \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
netstat is provided by the net-tools package,net-tools is probably not installed by default in the Docker image for Ubuntu 16.04 to keep the image size as small as possible.
Execute the following commands inside docker container:
apt update
apt install net-tools

Install vim in node Docker image

I'm trying to install vim in my image. I'm using node as base image:
FROM node
RUN apt-get update & apt-get install vim
//more things...
I get this error:
E: Unable to locate package vim
You are only using a single ampersand (&) in your RUN directive, which runs a command in the background in bash. Change it to include two ampersands (&&). Please also notice the -y (automatic yes to prompts) I have added to the apt-get statement, without which your docker build command will fail:
RUN apt-get update && apt-get install -y vim

Resources